var express = require('express');
var router = express.Router();
const {mongoFind, mongoInsert} = require('../db.js')
const multiparty = require('multiparty');
const path = require('path');

router.use(function(req, res, next) {
        console.log('-------打印sessionid-------')
        console.log(req.session.uid)
        console.log('-------res.locals.user------')
        if (req.session.uid && req.app.locals.user) {
            next()
        } else {
            res.redirect('/login')
        }
    })
    //增加商品 --GET--请求
router.get('/add', function(req, res, next) {
    res.render('addproduct')
});

//增加商品 --POST--请求
router.post('/add', function(req, res, next) {
    let form = new multiparty.Form();
    form.uploadDir = path.join(__dirname, '../upload');
    console.log('add print')
    form.parse(req, function(err, fields, files) {
        let title = fields[0].title;
        let fee = fields[0].fee;
        let description = fields[0].description;
        let price = fields[0].price;
        let imgPath = files.pic[0].path;

    });
    res.send('ok')
});

router.get('/edit', function(req, res, next) {
    res.render('editproduct')
});

router.post('/edit', function(req, res, next) {
    let formData = {}
});

router.get('/', function(req, res, next) { 
    mongoFind('product', {}, function(doc){
        console.log(doc)
        res.render('product',{products:doc});
    })
    
});
module.exports = router;